package com.example.fw;

import org.openqa.selenium.By;

public class AccountHelper extends WebDriverHelperBase
{

	public AccountHelper(ApplicationManager manager)
	{
		super(manager);
	}

	public void signup(User user)
	{
		openUrl("/");
		click(By.xpath(".//*[@class='bracket-link'][1]"));
		type(By.name("username"), user.getLogin());
		type(By.name("email"), user.getEmail());
		click(By.cssSelector("input.button"));

		if (isElementPresent(By.cssSelector("table.widht50 tbody tr td p")))
			throw new RuntimeException(driver.findElement(By.cssSelector("table.width50 tbody tr td p")).getText());

		pause(5000);
		/* Pochta rabotat ne hochet net soobsheniya v inbox, zato est v outgoing poka v try-catch*/
		try
		{
			Msg msg = manager.getMailHelper().getNewMail(user.getEmail(), user.getPassword());
			String confirmationLink = extractConfirmationLink(msg);

			openAbsluteUrl(confirmationLink);
			type(By.name("password"), user.getPassword());
			type(By.name("password_confim"), user.getPassword());
			click(By.cssSelector("input.button"));
		}
		catch (Exception ex)
		{
			click(By.xpath(".//span[@class='bracket-link']/a"));
			ex.printStackTrace();
		}
	}

	public boolean isloggedIn(User user)
	{
		String loggedUser = "";
		if (isElementPresent(By.cssSelector("td.login_info_left span")))
			loggedUser = driver.findElement(By.cssSelector("td.login_info_left span")).getText();

		return loggedUser.equals(user.getLogin());
	}

	public String extractConfirmationLink(Msg msg)
	{
		return msg.getConfirmationLink();
	}

	public void login(User user)
	{
		openUrl("/");
		type(By.name("username"), user.getLogin());
		type(By.name("password"), user.getPassword());
		click(By.cssSelector("input.button"));
	}

	public boolean isUserCreated(User admin, User user)
	{
		goToUserMenegmentPage(admin);
		findUser(user);
		return isElementPresent(By.xpath(".//form[@action='manage_user_delete.php']/input[@class='button']"));
	}

	public void deleteCreatedUser(User admin, User user)
	{
		if (!driver.getCurrentUrl().contains("manage_user_edit_page.php?username=" + user.getLogin()))
			goToUserMenegmentPage(admin);
		deleteSelectedUser(user);
	}

	private void deleteSelectedUser(User user)
	{
		if (!driver.getCurrentUrl().contains("manage_user_edit_page.php?username=" + user.getLogin()))
			findUser(user);

		click(By.xpath(".//form[@action='manage_user_delete.php']/input[@class='button']"));
		click(By.xpath(".//form[@action='manage_user_delete.php']/input[@class='button']"));
	}

	private void goToUserMenegmentPage(User admin)
	{
		if (!isloggedIn(admin))
		{
			logout();
			login(admin);
		}
		click(By.xpath(".//td[@class='menu']/a[contains(@href, 'manage_overview_page.php')]"));
		click(By.xpath(".//span[@class='bracket-link']/a[contains(@href, 'manage_user_page.php')]"));
	}

	private void findUser(User user)
	{
		System.out.println(user.getLogin());
		type(By.xpath(".//form[@action='manage_user_edit_page.php']/input[@type='text']"), user.getLogin());
		click(By.xpath(".//form/input[@type='submit'][@value='Manage User']"));
	}

	private void logout()
	{
		if (!isElementPresent(By.name("username")) && !isElementPresent(By.cssSelector("input.button")))
		{
			click(By.xpath(".//td[@class='menu']/a[contains(@href, 'logout_page.php')]"));
		}
	}
}
